﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Orf.Model;
using Orf.dto;
namespace Orf_system.收费业务
{
    public partial class FrmFpZf : Form
    {
        fapiaoZfDAO fpZfDAO = null;
        public FrmFpZf()
        {
            InitializeComponent();
            fpZfDAO = new fapiaoZfDAO();
        }

        private void Zhcz()
        {
            MZ_SFMX sfmxClass = new MZ_SFMX();
            MZ_ZFFP zffpClass = new MZ_ZFFP();
            MZ_CF01 cf01Class = new MZ_CF01();
            zffpClass.fPHM = txtfphm.Text;
            cf01Class.fPHM = txtfphm.Text;
            zffpClass.zFGH = FrmLogin.emp_sn;
            sfmxClass.fPHM = txtfphm.Text;
            sfmxClass.cZGH = FrmLogin.emp_sn;
            DataTable dt = null;
            dt = fpZfDAO.checkZdValue(sfmxClass, 1);
            try
            {
                if (dt.Rows.Count<=0)
                {
                    MessageBox.Show("你输入的发票号，没有需要作废的信息！");
                }
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        if (dr[0].ToString() != "")
                        {
                            MessageBox.Show("这张病人已经结账，发票不能作废");
                            break;
                        }
                        else if (dr[1].ToString() == "1")
                        {
                            MessageBox.Show("药房已经发药，发票不能作废");
                            break;
                        }
                        else if (dr[2].ToString() == "1")
                        {
                            MessageBox.Show("医技项目已经执行，发票不能作废");
                            break;
                        }
                    }
                    if (MessageBox.Show("确定要作废这张发票吗？", "作废提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        DialogResult = DialogResult.Yes;
                        int res = fpZfDAO.Inert_Zffp(zffpClass, 4);
                        if (res > 0)
                        {
                            MessageBox.Show("发票作废成功！");
                            fpZfDAO.Update_Sfmx(sfmxClass, 2);
                            fpZfDAO.Update_cf01(cf01Class, 5);
                        }
                        else
                        {
                            MessageBox.Show("发票作废失败");
                        }
                    }
                    else
                    {
                        DialogResult = DialogResult.No;
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
        
        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode==Keys.Enter)
            {
                Zhcz();
                if (dgvZfph.Rows.Count >= 0)
                    dgvZfph.Rows.Clear();
                FillDgv();
            }
        }

        private void FillDgv()
        {
            DataTable dt = null;
            dt=fpZfDAO.selectAllfpZfInfo();
            int intRows = dt.Rows.Count;
            int intColumns = dt.Columns.Count;
            if (intRows>=0)
            {
                for (int i = 0; i < intRows; i++)
                    dgvZfph.Rows.Add();
                for (int i = 0; i < intRows;i++ )
                {
                    for (int j = 0; j < intColumns;j++ )
                    {
                        dgvZfph.Rows[dgvZfph.CurrentRow.Index].Cells[j].Value = dt.Rows[i][j].ToString();
                    }
                }
            }
            else
            {
                return;
            }      
        }
        private void FrmFpZf_Load(object sender, EventArgs e)
        {
            FillDgv();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
